package com.sahala.downimage.client;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sahala.downimage.dao.IImageDAO;
import com.sahala.downimage.dao.IPageDAO;
import com.sahala.downimage.domain.ImageDO;
import com.sahala.downimage.domain.ImageQueryDO;

public class Reconciliation {
	
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context  = new ClassPathXmlApplicationContext(
				"spring-all.xml");
		IImageDAO imageDAO = (IImageDAO) context.getBean("imageDAO");
		IPageDAO pageDAO = (IPageDAO) context.getBean("pageDAO");
		
		ImageQueryDO queryDO = new ImageQueryDO();
		Calendar cal = Calendar.getInstance();
		cal.set(2012, 6, 1, 23, 59, 59);
		cal.set(Calendar.MILLISECOND, 0);
		queryDO.setGmtCreate(cal.getTime());
		List<ImageDO> list = imageDAO.queryAll(queryDO);
		
		
		File file = new File("E:\\pic\\20120702");
		List<String> files = new ArrayList<String>();
		if(file.isDirectory()){
			files = Arrays.asList(file.list());
		}
		
		for (ImageDO image : list) {
			if(files.contains(image.getImgName())){
				//System.out.println("normal for "+image.getImgName());
			} else {
				int imageDelCount = imageDAO.deleteById(image.getId());
				int pageDelCount = pageDAO.deleteBypageId(image.getPageId());
				if(imageDelCount != 1 || pageDelCount !=1){
					System.out.println("imageId:"+image.getId()+",delCount:"+imageDelCount+",pageId:"+image.getPageId()+",delCount:"+pageDelCount);
				}
			}
		}
		
		
		System.out.println(list.size());
	}
}
